package com.common.serialize;

import com.common.serialize.impl.JDKSerializer;
import com.common.serialize.impl.JsonSerializer;
import com.common.serialize.impl.KryoSerializer;

/**
 * @author Zixiang Hu
 * @description 定义通用的序列化接口
 * @create 2021-02-27-18:27
 */
public interface CommonSerializer {

    /**
     * 根据传入的code值返回不同的实现
     * @param code
     * @return
     */
    static CommonSerializer getByCode(int code) {
        switch (code) {
            case 0:
                return new KryoSerializer();
            case 1:
                return new JsonSerializer();
            case 2:
                return new JDKSerializer();
            default:
                return null;
        }
    }

    byte[] serialize(Object obj);

    Object deserialize(byte[] bytes, Class<?> clazz);

    /**
     * 返回当前序列化的code代码
     * 0：kryo
     * 1：json
     * 2：jdk
     * @return
     */
    int getCode();
}
